package com.neusoft.hifly.core.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;

import com.neusoft.hifly.core.token.JwtUtil;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2的接口配置
 */
@Configuration
@EnableSwagger2 // TODO 【技巧】开启springfox http://host:port/swagger-ui.html
@EnableKnife4j // 开启Swagger的增强注解 http://host:port/doc.html
public class SwaggerConfig {
	// 是否启用
	@Value(value = "${swagger.enable:true}")
	private boolean swaggerEnabled;

	@Autowired(required = false)
	private CompanyConfig company;

	@Autowired(required = false)
	private SystemConfig systemConfig;

	// 服务模块
	@Bean("v1")
	public Docket apiV1() {
		return getApiDocket("v1");
	}

	/**
	 * 根据服务模块封装
	 *
	 * @param groupName
	 *            服务模块
	 * @return 文档
	 */
	private Docket getApiDocket(final String groupName) {
		//添加head参数start
		final ParameterBuilder tokenPar = new ParameterBuilder();
		final List<Parameter> pars = new ArrayList<Parameter>();
		tokenPar.name(JwtUtil.TOKEN_KEY).description("令牌").modelRef(new ModelRef("string")).parameterType("header")
				.required(false);
		pars.add(tokenPar.build());
		//添加head参数end

		return new Docket(DocumentationType.SWAGGER_2).groupName(groupName).enable(swaggerEnabled).select()
				.apis(RequestHandlerSelectors.any())
				//				.paths(PathSelectors.any()) // 指定扫描哪些接口。PathSelectors.any()说明所有接口
				.paths(PathSelectors.ant("/api/" + groupName + "/**")).build().globalOperationParameters(pars)
				.apiInfo(apiInfo());
	}

	/**
	 * 文档信息
	 *
	 * @return 信息
	 */
	@SuppressWarnings("rawtypes")
	private ApiInfo apiInfo() {
		// 作者信息
		final Contact contact = new Contact(company.getCompanyName(), company.getCompanyWebsite(),
				company.getCompanyEmail());
		final ApiInfo apiInfo = new ApiInfo(systemConfig.getSysName() + "接口文档", // 标题
				systemConfig.getSysName(), // 描述
				"", // 版本
				company.getCompanyWebsite(), //（不可见）条款地址
				contact, // 作者
				company.getCompanyCopyright(), // 版权所有
				company.getCompanyWebsite(), // 公司网站链接
				new ArrayList<VendorExtension>());
		return apiInfo;
	}

	//	/**
	//	 * 配置swagger的ui页面
	//	 * <p>
	//	 * 注意了，在InterceptorConfiguration里面还有一段配置
	//	 */
	//	@Override
	//	public void addResourceHandlers(final ResourceHandlerRegistry registry) {
	//		// 解决swagger不显示问题
	//		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
	//		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	//		// Knife4j
	//		registry.addResourceHandler("doc.html").addResourceLocations("classpath*:/META-INF/resources/");
	//	}

}
